package cn.cxion.chat.app.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;

import com.lqr.imagepicker.ImagePicker;
import com.lqr.imagepicker.bean.ImageItem;
import com.lqr.optionitemview.OptionItemView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.activity.ConstantActivity;
import cn.cxion.chat.app.activity.DialogActivity;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.view.CircleImageView;
import cn.cxion.chat.kit.WfcUIKit;
import cn.cxion.chat.kit.common.OperateResult;
import cn.cxion.chat.kit.third.utils.ImageUtils;
import cn.cxion.chat.kit.user.UserViewModel;

public class MyFragment extends Fragment {


    @Bind(R.id.op_headportrait)
    OptionItemView opHeadportrait;
    @Bind(R.id.op_nickname)
    OptionItemView opNickname;
    @Bind(R.id.op_sex)
    OptionItemView opSex;
    @Bind(R.id.op_phone)
    OptionItemView opPhone;
    @Bind(R.id.op_email)
    OptionItemView opEmail;
    @Bind(R.id.op_post)
    OptionItemView opPost;
    @Bind(R.id.op_address)
    OptionItemView opAddress;
    @Bind(R.id.op_mycollection)
    OptionItemView opMycollection;
    @Bind(R.id.op_mydata)
    OptionItemView opMydata;
    @Bind(R.id.op_customerdata)
    OptionItemView opCustomerdata;
    @Bind(R.id.op_countandsafe)
    OptionItemView opCountandsafe;

    private static final int REQUEST_CODE_PICK_IMAGE = 100;
    @Bind(R.id.img_photo)
    CircleImageView imgPhoto;
    @Bind(R.id.op_dialog)
    OptionItemView opDialog;
    @Bind(R.id.test_check)
    OptionItemView testCheck;
    private UserViewModel userViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment_my, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);

    }

    @OnClick({R.id.op_headportrait, R.id.op_nickname, R.id.op_sex, R.id.op_phone, R.id.op_email, R.id.op_post, R.id.op_address, R.id.op_mycollection, R.id.op_mydata, R.id.op_customerdata, R.id.op_countandsafe, R.id.op_dialog, R.id.test_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.op_headportrait:
//                ConstantActivity.show(getActivity(), Constant.HEADPORTRAIT);
                ImagePicker.picker().pick(this, REQUEST_CODE_PICK_IMAGE);
                break;
            case R.id.op_nickname:
                ConstantActivity.show(getActivity(), Constant.NICKNAAME);
                break;
            case R.id.op_sex:
                ConstantActivity.show(getActivity(), Constant.SEX);
                break;
            case R.id.op_phone:
                ConstantActivity.show(getActivity(), Constant.PHONE);
                break;
            case R.id.op_email:
                ConstantActivity.show(getActivity(), Constant.EMAIL);
                break;
            case R.id.op_post:
                ConstantActivity.show(getActivity(), Constant.POST);
                break;
            case R.id.op_address:
                ConstantActivity.show(getActivity(), Constant.ADDRESS);
                break;
            case R.id.op_mycollection:
                ConstantActivity.show(getActivity(), Constant.MYCOLLECTION);
                break;
            case R.id.op_mydata:
                ConstantActivity.show(getActivity(), Constant.MYDATA);
                break;
            case R.id.op_customerdata:
                ConstantActivity.show(getActivity(), Constant.COMPANYDATA);
                break;
            case R.id.op_countandsafe:
                ConstantActivity.show(getActivity(), Constant.COUNTSAFE);
                break;
            case R.id.op_dialog:
                DialogActivity.show(getActivity());
                break;
            case R.id.test_check:
                ConstantActivity.show(getActivity(), Constant.DEMANDDETAILS);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            String imagePath = ImageUtils.genThumbImgFile(images.get(0).path).getAbsolutePath();
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, imgPhoto.getWidth(), imgPhoto.getHeight(), true);
            imgPhoto.setImageBitmap(mBitmap);
            imgPhoto.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            MutableLiveData<OperateResult<Boolean>> result = userViewModel.updateUserPortrait(imagePath);
            result.observe(this, booleanOperateResult -> {
                if (booleanOperateResult.isSuccess()) {
                    Toast.makeText(getActivity(), "更新头像成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "更新头像失败: " + booleanOperateResult.getErrorCode(), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
